package cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.entity.EquipmentClass;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.enums.EquipmentClassExceptionEnum;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.mapper.EquipmentClassMapper;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.pojo.request.EquipmentClassRequest;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.service.EquipmentClassService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 设备业务实现层
 *
 * @author PANL
 * @since 2023/12/01 14:12
 */
@Service
public class EquipmentClassServiceImpl extends ServiceImpl<EquipmentClassMapper, EquipmentClass> implements EquipmentClassService {

	@Override
    public void add(EquipmentClassRequest equipmentClassRequest) {
        EquipmentClass equipmentClass = new EquipmentClass();
        BeanUtil.copyProperties(equipmentClassRequest, equipmentClass);
        this.save(equipmentClass);
    }

    @Override
    public void del(EquipmentClassRequest equipmentClassRequest) {
        EquipmentClass equipmentClass = this.queryEquipmentClass(equipmentClassRequest);
        this.removeById(equipmentClass.getEqpClassCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(EquipmentClassRequest equipmentClassRequest) {
        this.removeByIds(equipmentClassRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(EquipmentClassRequest equipmentClassRequest) {
        EquipmentClass equipmentClass = this.queryEquipmentClass(equipmentClassRequest);
        BeanUtil.copyProperties(equipmentClassRequest, equipmentClass);
        this.updateById(equipmentClass);
    }

    @Override
    public EquipmentClass detail(EquipmentClassRequest equipmentClassRequest) {
        return this.queryEquipmentClass(equipmentClassRequest);
    }

    @Override
    public PageResult<EquipmentClass> findPage(EquipmentClassRequest equipmentClassRequest) {
        LambdaQueryWrapper<EquipmentClass> wrapper = createWrapper(equipmentClassRequest);
        Page<EquipmentClass> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<EquipmentClass> findList(EquipmentClassRequest equipmentClassRequest) {
        LambdaQueryWrapper<EquipmentClass> wrapper = this.createWrapper(equipmentClassRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    private EquipmentClass queryEquipmentClass(EquipmentClassRequest equipmentClassRequest) {
        EquipmentClass equipmentClass = this.getById(equipmentClassRequest.getEqpClassCode());
        if (ObjectUtil.isEmpty(equipmentClass)) {
            throw new ServiceException(EquipmentClassExceptionEnum.EQUIPMENT_CLASS_NOT_EXISTED);
        }
        return equipmentClass;
    }

    /**
     * 创建查询wrapper
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    private LambdaQueryWrapper<EquipmentClass> createWrapper(EquipmentClassRequest equipmentClassRequest) {
        LambdaQueryWrapper<EquipmentClass> queryWrapper = new LambdaQueryWrapper<>();

        String eqpClassCode = equipmentClassRequest.getEqpClassCode();
  /*      String eqpClassName = equipmentClassRequest.getEqpClassName();
        String eqpPrecision = equipmentClassRequest.getEqpPrecision();
        String specification = equipmentClassRequest.getSpecification();*/

        queryWrapper.like(ObjectUtil.isNotEmpty(eqpClassCode), EquipmentClass::getEqpClassCode, eqpClassCode);
     /*   queryWrapper.like(ObjectUtil.isNotEmpty(eqpClassName), EquipmentClass::getEqpClassName, eqpClassName);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpPrecision), EquipmentClass::getEqpPrecision, eqpPrecision);
        queryWrapper.like(ObjectUtil.isNotEmpty(specification), EquipmentClass::getSpecification, specification);*/

        return queryWrapper;
    }

}
